
#ifndef __HELLO_
#define __HELLO_
#include <stdio.h>
#include "main.h"

/* for key control */
#define EVENT_KEY_START 0x1
#define EVENT_KEY_TIMER 0x2
#define EVENT_KEY_USERSLEEP 0x4
#define EVENT_KEY_STEPMODE 0x8
#define EVENT_KEY_OUTDOOR 0x10
#define EVENT_KEY_INDOOR 0x20
extern void key_init();
extern int key_callback(int value);
extern uint8_t read_key();

/* for power control */
#define EVENT_POW_START 0X1
#define EVENT_POW_TIMER 0X2
extern void power_init();
extern int power_callback(int value);

/* for motor control */
#define EVENT_MOTOR_START 0x1
#define EVENT_MOTOR_TIMER 0x2
extern int motor_callback(int value);
extern void motor_open();
extern void motor_close();
extern void motor_init();
extern void motor_selftest();
extern void motor_shake(uint32_t times);

/* for led control */
#define EVENT_LED_START 0x1
#define EVENT_LED_TIMER 0x2
extern int led_callback(int value);
extern void led_open();
extern void led_close();
extern void led_init();
extern int led_callback(int value);
extern void led_selftest();
extern void led_set_mode(uint8_t mode);

/* for rtc control */
#define EVENT_RTC_START 0x1
#define EVENT_RTC_TIMER 0x2
#define EVENT_RTC_ALARM 0x4
#define EVENT_RTC_RECORD 0x10
#define EVENT_RTC_RECORD_END 0x20
#define EVENT_RTC_ALARM_EXIT 0x80
extern void rtc_init();
extern int rtc_callback(int value);
extern int rtc_set_alarm(uint8_t hour, uint8_t min, uint8_t sec);
extern uint8_t rtc_get_which_day();
/*  to turn it as the static function 
extern int rtc_set_curr_data(uint8_t year, uint8_t mon, uint8_t day);
extern int rtc_set_curr_time(uint8_t hour, uint8_t min, uint8_t sec);
*/
extern int rtc_sync_func(uint8_t year, uint8_t mon, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec, uint8_t weekday);

/* for uart control */
#define EVENT_UART_DATA_COME 0x1
extern void uart_init();
extern int uart_callback(int value);
extern void USART1_SendString(uint8_t *data, uint32_t size);

/* for runflash */
typedef struct 
{
    uint8_t type;
    uint8_t time[6];
}rtc_log_t;


#define SAVE_HOURS 24
typedef struct
{
    uint32_t StepSAtHour[SAVE_HOURS];
    uint32_t CaliAtHour[SAVE_HOURS];
    uint32_t SleepAtHour[SAVE_HOURS];
    
}UserDataTable_t;

typedef struct
{
    uint8_t deviceid[16];
    uint8_t LastShutIsSleep;
    uint8_t IsNewDisk;
    rtc_log_t *NodeHead;
    
    uint8_t current_off;
    
    uint8_t user_height;
    uint8_t user_weight;
    uint8_t percent_target;
    uint8_t target_dist;
    uint8_t per_step_dist;
    
    uint8_t CurrIndex72;
    UserDataTable_t UD_table;
    
}SKY_t;

extern void runflash_init();
extern void create_rtc_space(uint8_t *src, int len);
extern void save_data(uint8_t *addr, uint8_t *data, uint8_t size_data);

/* for battery control */
#define EVENT_VOL_START 0x1
#define EVENT_VOL_TIMER 0x2
extern void battery_init();
extern int battery_callback(int value);
extern uint32_t get_battery_value();

/* for transmiter control */
extern void Tdevice_open();
extern void Tdevice_close();

/* for delay module */
extern void Delayms(__IO uint32_t nTime);

/* for sensor control */
extern void sensor_init();

/* for motion prog */
extern int motion_know(int mode, int clearmark);

/* for evern loop setting */

#define LED_TASK  1
#define KEY_TASK  0
#define MOTOR_TASK 2
#define BATTERY_TASK 3
#define UART_TASK 4
#define RTC_TASK  5
#define COOLDOG_TASK 6
#define APP_TASK 7
#define STATICS_TASK 8
#define POWER_TASK 9
extern void set_event(int taskid, int evt_type);
extern void set_timer_event(int taskid, int evt_type, uint32_t delay_time);

/* for cooldog control */
#define EVENT_CDOG_TIMER 0x1
extern void cooldog_init();
extern int cooldog_callback(int value);

/* for app event */
#define EVENT_APP_START 0x1
#define EVENT_APP_TIMER 0x2
typedef enum { 
     STEP_MODE = 0,
     SLEEP_MODE,
     DMOTION_MODE,
     WAIT_MODE
}MODE_t;


extern int app_callback(int value);
extern void set_currapp_mode(uint8_t value);
extern uint8_t get_currapp_mode();

/* for clock setting */
extern void clock_init();

/* for data */
#define EVENT_DATA_START 0x1
#define EVENT_DATA_UPDATE_HR 0x2
#define EVENT_DATA_TIMER 0x4
extern void get_outdoor_data(uint8_t *data);
extern void get_indoor_data(uint8_t *data);
extern void sync_time_log();
extern int statisc_callback(int value);
extern void step_data_buffing(uint32_t stepnum, uint32_t cali, uint32_t distan);

/* for control 2.4 device */
extern void Tdevice_open();
extern void Tdevice_close();
#endif